<template>
  <div id="addQuestionView">创建题目</div>
  <a-form :model="form">
    <a-form-item field="title" label="题目标题">
      <a-input v-model="form.title" placeholder="请输入题目标题" />
    </a-form-item>
    <a-form-item field="tags" label="题目标签">
      <a-input-tag v-model="form.tags" allow-clear placeholder="请输入题目标签" />
    </a-form-item>
    <a-form-item field="answer" label="答案">
      <MdEditor :handle-change="onAnswerChange" :value="form.answer" />
    </a-form-item>
    <a-form-item field="content" label="题目内容">
      <MdEditor :handle-change="onContentChange" :value="form.content" />
    </a-form-item>
    <a-form-item :content-flex="false" :merge-props="false" label="题目配置">
      <a-space direction="vertical" style="min-width: 480px">
        <a-form-item field="judgeConfig.memoryLimit" label="内存限制">
          <a-input-number v-model="form.judgeConfig.memoryLimit" min="0" mode="button" placeholder="请输入内存限制" size="large" />
        </a-form-item>
        <a-form-item field="judgeConfig.stackLimit" label="堆栈限制">
          <a-input-number v-model="form.judgeConfig.stackLimit" min="0" mode="button" placeholder="请输入堆栈限制" size="large" />
        </a-form-item>
        <a-form-item field="judgeConfig.timeLimit" label="时间限制">
          <a-input-number v-model="form.judgeConfig.timeLimit" min="0" mode="button" placeholder="请输入时间限制" size="large" />
        </a-form-item>
      </a-space>
    </a-form-item>
    <a-form-item :content-flex="false" :merge-props="false" label="测试用例配置">
      <a-form-item v-for="(judgeCaseItem, index) of form.judgeCase" :key="index" no-style>
        <a-space direction="vertical" style="min-width: 640px">
          <a-form-item :key="index" :field="`form.judgeCase[${index}].value`" :label="`输入用例-${index + 1}`">
            <a-input v-model="judgeCaseItem.intput" placeholder="请输入测试输入用例" />
          </a-form-item>
          <a-form-item :key="index" :field="`form.judgeCase[${index}].value`" :label="`输出用例-${index + 1}`">
            <a-input v-model="judgeCaseItem.output" placeholder="请输入测试输出用例" />
          </a-form-item>
          <a-button status="danger" @click="handleDelete(index)">删除</a-button>
        </a-space>
      </a-form-item>
      <div style="margin-top: 32px">
        <a-button status="success" type="outline" @click="handleAdd()">新增用例</a-button>
      </div>
    </a-form-item>
    <a-form-item>
      <a-button style="min-width: 16px" type="primary" @click="doSubmit()">提交</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import MdEditor from '@/components/MdEditor.vue';
import { QuestionControllerService } from '../../../generated';
import message from '@arco-design/web-vue/es/message';
import { useRoute } from 'vue-router';

const route = useRoute();
const updatePage = route.path.includes('update');

const form = ref({
  answer: '',
  content: '',
  tags: [],
  title: '',
  judgeCase: [
    {
      intput: '1 2',
      output: '3 4'
    }
  ],
  judgeConfig: {
    memoryLimit: 1000,
    stackLimit: 1000,
    timeLimit: 1000
  }
});

const id = route.query.id;

/**
 * 根据题目id 获取老的数据
 */
const loadData = async () => {
  if (!id) {
    return;
  }
  const res = await QuestionControllerService.getQuestionByIdUsingGet(id as any);
  if (res.code === 0) {
    form.value = res.data as any;
    console.log(form);
    // json 转 js 对象
    if (!form.value.judgeCase) {
      form.value.judgeCase = [
        {
          intput: '',
          output: ''
        }
      ];
    } else {
      form.value.judgeCase = JSON.parse(form.value.judgeCase as any);
    }
    if (!form.value.judgeConfig) {
      form.value.judgeConfig = {
        memoryLimit: 1000,
        stackLimit: 1000,
        timeLimit: 1000
      };
    } else {
      form.value.judgeConfig = JSON.parse(form.value.judgeConfig as any);
    }
    if (!form.value.tags) {
      form.value.tags = [];
    } else {
      form.value.tags = JSON.parse(form.value.tags as any);
    }
  } else {
    message.error('加载失败，' + res.message);
  }
};

onMounted(() => {
  loadData();
});

const doSubmit = async () => {
  console.log(form.value);
  //区分更新还是创建
  if (updatePage) {
    const res = await QuestionControllerService.updateQuestionUsingPost(form.value);
    if (res.code === 0) {
      message.success('更新成功');
    } else {
      message.error('更新失败' + res.message);
    }
  } else {
    const res = await QuestionControllerService.addQuestionUsingPost(form.value);
    if (res.code == 0) {
      message.success('创建成功');
    } else {
      message.error('创建失败' + res.message);
    }
  }
};

/**
 * 新增测试用例
 */
const handleAdd = () => {
  form.value.judgeCase.push({
    intput: ' ',
    output: ' '
  });
};

/**
 * 删除测试用例
 */
const handleDelete = (index: number) => {
  form.value.judgeCase.splice(index, 1);
};

const onAnswerChange = (value: string) => {
  form.value.answer = value;
};

const onContentChange = (value: string) => {
  form.value.content = value;
};
</script>

<style scoped>
#addQuestionView {
}
</style>
